package org.wattdepot.cli.command;

import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.cli.CommandLineInterface;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Returns the total carbon that would be emitted by that source over an hour at the rate 
 * occurring as of the timestamp.
 * 
 * @author Anthony Xu & John Mack
 *
 */
public class CarboncontentCommand implements WattDepotCliCommand {
  
  /**
   * Returns the total carbon that would be emitted by that source over an hour at the rate 
   * occurring as of the timestamp.
   * 
   * @param cli WattDepotClient with hostUri = http://server.wattdepot.org:8182/wattdepot/
   * @param commands user's command line input
   * @return string to output
   */
  public String doCommand(CommandLineInterface cli, List<String> commands) {
    String str = "";
    try {
      XMLGregorianCalendar timestamp = Tstamp.makeTimestamp(commands.get(2));
      XMLGregorianCalendar startTime = timestamp;
      XMLGregorianCalendar endTime = Tstamp.incrementHours(startTime, 1);
      int interval = Integer.valueOf(commands.get(4));
      double energy = cli.getClient().getEnergyGenerated(commands.get(1), startTime, endTime, 
        interval);
      double carbon = cli.getClient().getCarbonEmitted(commands.get(1), startTime, endTime, 
        interval);
      energy = energy / 1000000;
      double carbonContent = carbon / energy;
      str += Double.toString(carbonContent);
      str += " lbs CO2/MWh";
    }
    catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return str;
  }

}
